package com.tongji.seller.view
{
	import com.adobe.images.JPGEncoder;
	import com.tongji.seller.events.AddImageEvent;
	import com.tongji.seller.events.AddStoreSceneEvent;
	import com.tongji.seller.events.ChangePropertyEvent;
	import com.tongji.seller.events.GetStoreItemInfoEvent;
	import com.tongji.seller.events.GetStoreItemsEvent;
	import com.tongji.seller.events.GetStoreSceneDetailEvent;
	import com.tongji.seller.events.HideFurnitureEvent;
	import com.tongji.seller.events.ShowRoomEvent;
	import com.tongji.seller.events.utilsEvent.ChangeStepEvent;
	import com.tongji.seller.events.utilsEvent.ListItemMouseEvent;
	import com.tongji.seller.model.FurnitureModel;
	import com.tongji.seller.model.GetSystemSceneModel;
	import com.tongji.seller.model.StoreProductModel;
	import com.tongji.seller.model.StoreSceneModel;
	import com.tongji.seller.model.UserModel;
	import com.tongji.seller.model.UserSceneModel;
	import com.tongji.seller.utils.DragDropManager;
	import com.tongji.seller.utils.PopUpAdmin;
	import com.tongji.seller.utils.Showroom;
	import com.tongji.seller.view.twoD.Furniture;
	import com.tongji.seller.vo.UserAngle;
	
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	import mx.utils.Base64Encoder;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 *
	 * @author caddy
	 * @date 2011-12-8
	 */
	public class ThirdStepViewMediator extends Mediator
	{
		[Inject]
		public var view:ThirdStepView;
		
		[Inject]
		public var getSystemSceneModel:GetSystemSceneModel;
		
		[Inject]
		public var furnitureModel:FurnitureModel;
		
		[Inject]
		public var storeProductModel:StoreProductModel;
		
		[Inject]
		public var saveModel:UserSceneModel;
		
		[Inject]
		public var storeSceneModel:StoreSceneModel;
		
		[Inject]
		public var userModel:UserModel;
		
		public var selectedScene:Object = new Object(); //某个场景，Object类型，45度或者90度或者135度
		private var wholeScene:Object = new Object(); //整个场景，Object类型，包含三个defaultScene
		private var selectedAngle:int = 0; //所选中的角度，int类型
		private var sceneAngle:Object = new Object();
		private var user45:UserAngle;
		private var user135:UserAngle;
		private var user90:UserAngle;
		
		private var pageIndexCount:int = 1;
		private var productCount:int;
		
		override public function onRegister():void
		{
			view.sceneList.selectedIndex = 0;
			getSystemSceneModel.tempSceneArray = new ArrayCollection();

			BindingUtils.bindProperty(view.sceneList, "dataProvider", storeSceneModel, "selectedSceneArray");
			BindingUtils.bindProperty(view.productList, "dataProvider", storeProductModel, "storeProduct");
			
			eventMap.mapListener(view.reSelectSceneBtn, MouseEvent.CLICK, reSelectSceneHandler, MouseEvent);
			eventMap.mapListener(view.reSelectProBtn, MouseEvent.CLICK, reSelectProHandler, MouseEvent);
			
			eventMap.mapListener(view.preBtn, MouseEvent.CLICK, preBtnHandler, MouseEvent);
			eventMap.mapListener(view.nextBtn, MouseEvent.CLICK, nextBtnHandler, MouseEvent);
			
			eventMap.mapListener(view.sceneList, IndexChangeEvent.CHANGE, selectSceneHandler, IndexChangeEvent);
			eventMap.mapListener(view.productList, ListItemMouseEvent.ITEM_MOUSE_DOWN, itemMouseDownHandler, ListItemMouseEvent);

			addViewListener(ShowRoomEvent.SHOW_ROOM, showRoomHandler);
			this.addContextListener(ShowRoomEvent.SHOW_ROOM, showRoomHandler);
			addContextListener(GetStoreItemInfoEvent.UPDATE_STORE_ITEM, test, GetStoreItemInfoEvent);
			
			//打开该页面默认展示第一个场景的第一个角度
			if (getSystemSceneModel.selectSceneArray.length > 0)
			{
				dispatch(new ShowRoomEvent(ShowRoomEvent.SHOW_ROOM, getSystemSceneModel.selectSceneArray[0]));
				view.sceneList.selectedIndex = 0;
			}
			
			//addContextListener(AddImageEvent.UPDATE_IMAGE, addUserSceneHandler, AddImageEvent);

			if(storeProductModel.selectedProductArray.length % 10 == 0)
			{
				productCount = storeProductModel.selectedProductArray.length / 10;
			}
			else
			{
				productCount = storeProductModel.selectedProductArray.length / 10 + 1;
			}
			
			/*view.editScene.angle45.createScene(storeSceneModel.angle45Scene);*/			
			mediatorMap.createMediator(view.editScene);
			//mediatorMap.createMediator(view.editScene.angle45);	
			addContextListener(GetStoreSceneDetailEvent.UPDATE_SCENE_DETAIL, updateSucHandler, GetStoreSceneDetailEvent);	

		}

		private function updateSucHandler(event:GetStoreSceneDetailEvent):void
		{
			mediatorMap.createMediator(view.editScene);
			//view.viewstack.selectedIndex = 0;
			//view.angle45Btn.selected = true;
			//mediatorMap.createMediator(view.angle45.roomDisplay);
			/*selectedScene = storeSceneModel.sceneDetail;
			view.angle45.createScene(storeSceneModel.angle45Scene);
			for each (var angle:Object in storeSceneModel.sceneDetail.SceneAngleList)
			{
				switch (angle.Jdid)
				{
					case 31:
						angle45Scene = angle;
						view.angle45.createScene(angle);
						break;
					case 30:
						angle135Scene = angle;
						//view.angle135.createScene(angle);
						break;
					case 35:
						angle90Scene = angle;
						//view.angle90.createScene(angle);
						break;
				}
			}	*/		
		}
		/**
		 * 弹出重新选择场景的弹出框 
		 * @param event
		 */		
		private function reSelectSceneHandler(event:MouseEvent):void
		{
			storeSceneModel.state = 1;
			dispatch(new ChangeStepEvent(ChangeStepEvent.TO_FIRST_STEP));			
		}
		
		/**
		 * 弹出重新选择宝贝的弹出框 
		 * @param event 
		 */	
		private function reSelectProHandler(event:MouseEvent):void
		{
			dispatch(new ChangeStepEvent(ChangeStepEvent.TO_SECOND_STEP));
		}
		
		/**
		 * 上一页宝贝 
		 * @param event
		 * 
		 */		
		private function preBtnHandler(event:MouseEvent):void
		{
			if(pageIndexCount > pageIndexCount)
			{
				pageIndexCount = pageIndexCount - 2;
			}
			if(pageIndexCount > 1)
			{
				dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_ITEM, userModel.currentUserID, pageIndexCount, 10));
				pageIndexCount--;
			}
		}
		
		/**
		 * 下一页宝贝 
		 * @param event
		 * 
		 */		
		private function nextBtnHandler(event:MouseEvent):void
		{
			if(pageIndexCount == 1)
			{
				pageIndexCount = 2;
			}
			if(pageIndexCount <= productCount)
			{
				dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_ITEM, userModel.currentUserID, pageIndexCount, 10));
				pageIndexCount++;
			}
		}
		
		/**
		 * 选择场景 
		 * @param event
		 * 
		 */		
		private function selectSceneHandler(event:IndexChangeEvent):void
		{
			mediatorMap.removeMediatorByView(view.editScene);
			dispatch(new GetStoreSceneDetailEvent(GetStoreSceneDetailEvent.GET_SCENE_DETAIL, Number(view.sceneList.selectedItem.sid)));

		  /*  if (view.editScene.angle135 != null)
				view.editScene.angle135.clearHandler();*/
			/*if (view.editScene.angle45 != null)
				view.editScene.angle45.clearHandler();*/
			/*if (view.editScene.angle90 != null)
				view.editScene.angle90.clearHandler();*/
		}
		
		public function showRoomHandler(event:ShowRoomEvent):void
		{		
			storeSceneModel.sceneDetail = null;
			/*			dispatch(new GetStoreSceneDetailEvent(GetStoreSceneDetailEvent.GET_SCENE_DETAIL, event.data.sid));
		*/		
		}
		
		private function test(event:GetStoreItemInfoEvent):void
		{
			//BindingUtils.bindProperty(view.productList, "dataProvider", storeProductModel, "storeProductArray");
		}
		
		/**
		 * 拖动宝贝
		 * @param event
		 * 
		 */		
		private function itemMouseDownHandler(event:ListItemMouseEvent):void
		{
			storeProductModel.arrarIndex = event.index;
			furnitureModel.resetImges(event.target.data);
			furnitureModel.furniture = new Furniture(event.target.data, event.content);
			DragDropManager.startDragFurniture(view, event.myEvent, new Furniture(event.target.data, event.content));
		}

	}
}